package com.privacy.accountmgr.views;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.AttributeSet;

import com.privacy.accountmgr.R;

import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

public class MySwitchCompat extends SwitchCompat {
    public MySwitchCompat(Context context) {
        super(context);
    }

    public MySwitchCompat(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwitchCompat(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setColors(int textColor, int accentColor, int backgroundColor) {
        setTextColor(textColor);
        int[][] states = new int[][]{{-android.R.attr.state_checked}, {android.R.attr.state_checked}};
        int[] thumbColors = new int[]{ContextCompat.getColor(getContext(), R.color.thumb_deactivated), accentColor};
        int[] trackColors = new int[]{ContextCompat.getColor(getContext(), R.color.track_deactivated), adjustAlpha(accentColor, 0.3f)};

        DrawableCompat.setTintList(DrawableCompat.wrap(getThumbDrawable()), new ColorStateList(states, thumbColors));
        DrawableCompat.setTintList(DrawableCompat.wrap(getTrackDrawable()), new ColorStateList(states, trackColors));
    }


    public int adjustAlpha(int color, float factor) {
        int alpha = Math.round(Color.alpha(color) * factor);
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        return Color.argb(alpha, red, green, blue);
    }
}
